/*
 * ScoreKeeper class is in charge of keeping track of:
 * strikes, balls, outs, innings and score, hits and errors during a game.
 */

#include "scorekeeper.h"

using namespace std;

/*
 *    //constant values
 *   const int REGULATION_INNINGS=9;
 *   const int MAX_NUMBER_OF_STRIKES=3;
 *   const int MAX_NUMBER_OF_BALLS=4;
 *   const int MAX_NUMBER_OF_OUTS=3;
 *
 *  private:
 *   int homeScore;
 *   int awayScore;
 *   int homeHits;
 *   int awayHits;
 *   int homeErrors;
 *   int awayErrors;
 *   int strikes;
 *   int balls;
 *   int outs;
 *   int inning;
 *   bool bottomHalfOfInning;//needed to know when to increment inning(inning = 2 halves, away team bats, home team bats) bottom of the inning is when the home team bats.
 */


ScoreKeeper::ScoreKeeper()
{
    homeScore=0;
    awayScore=0;
    homeHits=0;
    awayHits=0;
    homeErrors=0;
    awayErrors=0;
    strikes=0;
    balls=0;
    outs=0;
    inning=0;
    bottomHalfOfInning=false;
    REGULATION_INNINGS=9;
    MAX_NUMBER_OF_STRIKES=3;
    MAX_NUMBER_OF_BALLS=4;
    MAX_NUMBER_OF_OUTS=3;
}

ScoreKeeper::~ScoreKeeper(){

}

int ScoreKeeper::getHomeScore(){
    return homeScore;
}

int ScoreKeeper::getAwayScore(){
    return awayScore;
}

//adds runs to the batting team
void ScoreKeeper::addToScore(int runs){
    if(isBottomHalf()){
        homeScore+=runs;
    }
    else{
        awayScore+=runs;
    }
}

int ScoreKeeper::getHomeHits(){
    return homeHits;
}

int ScoreKeeper::getAwayHits(){
    return awayHits;
}

//adds hits to the batting team;
void ScoreKeeper::incrementHits(){
    if(isBottomHalf()){
        homeHits++;
    }
    else{
        awayHits++;
    }
}

int ScoreKeeper::getHomeErrors(){
    return homeErrors;
}

int ScoreKeeper::getAwayErrors(){
    return awayErrors;
}

//adds errors to the pitching team
void ScoreKeeper::addToErrors(int errors){
    if(isBottomHalf()){
        awayErrors+=errors;
    }
    else{
        homeErrors+=errors;
    }
}

int ScoreKeeper::getStrikes(){
    return strikes;
}

int ScoreKeeper::getBalls(){
    return balls;
}

int ScoreKeeper::getOuts(){
    return outs;
}

int ScoreKeeper::getInning(){
    return inning;
}

bool ScoreKeeper::isBottomHalf(){
    return bottomHalfOfInning;
}


void ScoreKeeper::incrementStrikes(){
    if(strikes==2){
        resetAtBat();
        incrementOuts();
    }
    else{
        strikes++;
    }
}

void  ScoreKeeper::incrementBalls(){
    if(balls==3){
        resetAtBat();
    }
    else{
        balls++;
    }
}

void ScoreKeeper::incrementOuts(){
    if(outs==2){
        if(isBottomHalf()){
            incrementInning();
        }
        else{
            resetInning();
        }
    }
    else{
        outs++;
    }
}

void ScoreKeeper::incrementInning(){
   resetInning();
   inning++;
}

void ScoreKeeper::toggleBottomHalf(){
    bottomHalfOfInning= !bottomHalfOfInning;
}

bool ScoreKeeper::isGameOver(){
    return inning >= REGULATION_INNINGS && homeScore!= awayScore && !bottomHalfOfInning;
}

void ScoreKeeper::resetAtBat(){
    strikes=0;
    balls=0;
}

void ScoreKeeper::resetInning(){
    outs=0;
    resetAtBat();
    toggleBottomHalf();
}

void ScoreKeeper::foulBall(){
    if(strikes<2){
       incrementStrikes();
    }
}

